package com.example.common.gamedetail;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;

import com.example.common.R;
import com.example.common.databinding.ActivityGameDetailBinding;
import com.example.common.user.GlobalUser;

public class GameDetailActivity extends AppCompatActivity {
    private static final String TAG = "GameDetailActivity";
    private ActivityGameDetailBinding binding;
    private static final String BASE_URL = "https://www.tlianfou.top/batbat/#/batbat/gameDetail/id/token?id=";

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //binding数据绑定
        binding = DataBindingUtil.setContentView(this, R.layout.activity_game_detail);

        //获取传过来的数据
        int id = getIntent().getExtras().getInt("ID");

        Log.i(TAG, "onCreate: " + id);

        //webView设置使用JavaScript
        WebSettings webSettings = binding.commonGameDetailWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //自适应屏幕
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        //监控返回
        binding.commonGameDetailWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && binding.commonGameDetailWebView.canGoBack()) {
                        //表示按返回键时的操作
                        binding.commonGameDetailWebView.goBack();
                        return true;
                    }
                }
                return false;
            }
        });
        //加载链接
        StringBuilder builder = new StringBuilder();
        builder.append(BASE_URL).append(id).append("&token=").append(GlobalUser.TOKEN);
        Log.i(TAG, "onCreate: " + builder.toString());
        binding.commonGameDetailWebView.loadUrl(builder.toString());
    }
}